package com.kaifamiao.wendao.servlet;


import com.kaifamiao.wendao.entity.Topic;
import com.kaifamiao.wendao.service.TopicService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet( "/topic/update/*" )
public class TopicUpdateServlet extends HttpServlet {

    @Override
    protected void doGet( HttpServletRequest request , HttpServletResponse response)
            throws ServletException, IOException {

        // 检查用户是否登录

        String uri = request.getRequestURI();

        String idString = "" ;
        int last = uri.lastIndexOf( "/" );
        if( last != uri.length() - 1 ) {
            idString = uri.substring( last + 1 );
        }

        Integer topicId = null ;
        // 如果 idString 表示的是整数
        if( idString.matches( "^\\d+$" )){
            topicId = Integer.valueOf( idString );
        }

        TopicService ts = new TopicService();

        Topic t = ts.load( topicId );

        // 将 话题 设置到 请求对象 的属性中
        request.setAttribute( "topic" , t );

        String path = "/WEB-INF/pages/topic/update.jsp" ;
        // 去往详情页面
        RequestDispatcher dispatcher = request.getRequestDispatcher( path );
        dispatcher.forward( request , response );
    }

    @Override
    protected void doPost( HttpServletRequest request , HttpServletResponse response)
            throws ServletException, IOException {
        // 完成数据校验

        // 将修改后的话题(标题和内容)同步到数据库

    }
}
